package com.ftdigital.primemovers_operators.util;

import android.content.Context;
import android.util.TypedValue;

/**
 * Created by Curry on 2016-1-13.
 * 显示的转换工具类
 */
public class DisplayUtil {

    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale);//+0.5是为了强制转换的时候四舍五入
    }

    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5);
    }

    public static int sp2px(Context context, float spValue) {
        final float fontSCale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontSCale + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        final float fontSCale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontSCale + 0.5f);
    }

    public static int dp2px(Context context, int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dp,
                context.getResources().getDisplayMetrics());
    }


    public static int sp2px(Context context, int sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                sp,
                context.getResources().getDisplayMetrics());
    }
}
